package com.bangf.service.impl;

import com.bangf.domain.*;
import com.bangf.repo.*;
import com.bangf.service.SelectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SelectServiceImpl implements SelectService {

    @Autowired
    private TownRepo townR;
    @Autowired
    private VillageRepo villageR;
    @Autowired
    private VillageSumRepo villageSumR;
    @Autowired
    private TownSumRepo townSumR;
    @Autowired
    private HouseholdRepo householdR;
    @Autowired
    private ResidentRepo residentR;
    @Autowired
    private SatisfyRepo satisfyR;
    @Autowired
    private EvaluationRepo evaluationR;
    @Autowired
    private  SumRepo sumR;

    @Override
    public List<FpTown> findAllTown() {
        return townR.findAll();
    }

    @Override
    public List<FpVillage> findAllVillageByTownId(Integer townId) {
        return villageR.findAllByTownId(townId);
    }

    @Override
    public boolean checkHasTown(Integer townId) {
        FpTown town = townR.findOne(townId);
        return town != null;
    }

    @Override
    public boolean checkHasVillage(Integer villageId) {
        FpVillage village = villageR.findOne(villageId);
        return village != null;
    }

    @Override
    public FpVillageSum findVillageSumByVillageId(Integer villageId) {
        return villageSumR.findOne(villageId);
    }

    @Override
    public FpTownSum findTownSumByTownId(Integer townId) {
        return townSumR.findOne(townId);
    }

    @Override
    public List<FpHousehold> findHouseholdByTownId(Integer townId) {
        return householdR.findByTownId(townId);
    }

    @Override
    public Long countAllHouseHold() {
        return householdR.count();
    }

    @Override
    public List<FpHousehold> findHouseHoldLimit(Integer index, Integer limit) {
        return householdR.findLimit(index,limit);
    }

    @Override
    public Integer countResidentByHouseholdId(Integer householdId) {
        return residentR.countByHouseholdId(householdId);
    }

    @Override
    public Integer sumHouseholdAllIncomeByTownId(Integer townId) {
        return householdR.sumAllIncomeByTownId(townId);
    }

    @Override
    public Integer countHouseholdByTownId(Integer townId) {
        return householdR.countAllIncomeByTownId(townId);
    }

    @Override
    public Integer countResidentByTownId(Integer townId) {
        return residentR.countByTownId(townId);
    }

    @Override
    public List<FpTownSum> findAllTownSum() {
        return townSumR.findAll();
    }

    @Override
    public boolean checkHasTownRank(Integer rank) {
        return townR.count() >= rank;
    }

    @Override
    public List<FpTownSum> findTownSumOrderByAllIncome(Integer rank) {
        return townSumR.findOrderByAllIncome(rank);
    }

    @Override
    public List<FpVillageSum> findVillageSumOrderByAllIncome(Integer rank) {
        return villageSumR.findOrderByAllIncome(rank);
    }

    @Override
    public boolean checkResidentWbByHouseholdId(Integer householdId) {
        return residentR.findWbByHouseholdId(householdId) != null;
    }

    @Override
    public FpSatisfy findSatisfyByTownId(Integer townId) {
        return satisfyR.findByTownId(townId);
    }

    @Override
    public FpEvaluation findEvaluationByTownId(Integer townId) {
        return evaluationR.findByTownId(townId);
    }

    @Override
    public Integer countHelperByTownId(Integer townId) {
        Integer count = householdR.countHelperByTownId(townId);
        return count;
    }

    @Override
    public FpSum findSumLimitOne() {
        return sumR.findLimitOne();
    }

    @Override
    public Integer countPoorVillageByTownId(Integer townId) {
        Integer count = villageR.countPoorVillage(townId);
        return count;
    }

    @Override
    public FpTown findOneTown(Integer townId) {
        return townR.findOne(townId);
    }

    @Override
    public Integer countCardHouseholdByTownId(Integer townId) {
        return householdR.countCardByTownId(townId);
    }

    @Override
    public Integer countHouseholdAvgIncomeByTownId(Integer nhType, Integer townId) {
        Integer allIncome = householdR.sumAllIncomeByNhTypeAndTownId(nhType,townId);
        Integer householdSum = householdR.countAllIncomeByNhTypeAndTownId(nhType,townId);
        if(householdSum == null || householdSum == 0) return 0;
        return allIncome/householdSum;
    }

    @Override
    public Long countResident() {
        return residentR.count();
    }

    @Override
    public List<FpResident> findResidentLimit(Integer index, Integer limit) {
        return residentR.findLimit(index,limit);
    }

    @Override
    public FpSum findOneSum() {
        return sumR.findLimitOne();
    }

    @Override
    public List<FpVillageSum> findAllVillageSum() {
        return villageSumR.findAll();
    }

    @Override
    public Integer sumHouseholdAllIncomeByVillageId(Integer villageId) {
        return householdR.sumAllIncomeByVillageId(villageId);
    }

    @Override
    public FpVillage findOneVillage(Integer villageId) {
        return villageR.findOne(villageId);
    }

    @Override
    public List<FpHousehold> findHouseholdByName(String name) {
        return householdR.findByHzName(name);
    }

    @Override
    public List<FpHousehold> findHouseholdByIdCard(String idCard) {
        return householdR.findByIdCard(idCard);
    }

    @Override
    public List<FpResident> findResidentByHouseholdId(Integer householdId) {
        return residentR.findByHouseholdId(householdId);
    }


}
